package ctrlLayer;

import dbLayer.DbInvoice;
import modelLayer.Invoice;
import modelLayer.SalesOrder;

public class CtrInvoice {

	private DbInvoice dbi;

	public CtrInvoice(){
		dbi = new DbInvoice();
	}

	/**
	 * Method for adding a order to a invoice.
	 * @param soObj The order to be added to the invoice. The invoice is found through the customer associated to the order.
	 */
	public void addOrderToInvoice(SalesOrder soObj){
		Invoice invObj = dbi.retrieveInvoiceFromDb(soObj.getCustomer().getPartnerId());
		if(invObj != null){
			invObj.addSalesOrder(soObj);
			soObj.setInvoiceNo(invObj.getInvoiceNo());
			//Updater invoice i databasen!
		}
		else {
			Invoice ninvObj = new Invoice(soObj);
			try {
				dbi.insertInvoice(ninvObj);
			} catch (Exception e) {
				System.out.println("Error inserting invoiceObject in CtrInvoice");
			}
		}
	}

}
